;# read for-r file and generate R file to plot graph using R

set file for-r-notabs
set fptr [open $file r]
  gets $fptr firstln
  set stocks [split $firstln { }]
close $fptr

puts {stock_data <- read.table("for-r-notabs", header=T, sep=" ")}
puts {max_y <- max(stock_data)}
puts {plot_colors <- c("blue","red","forestgreen")}
# puts {png(filename="figure.png", height=480, width=640, bg="white")}

set j 0
puts "plot(stock_data\$[lindex $stocks 0], type=\"o\", col=plot_colors\[[incr j]\], ylim=c(0,max_y), axes=FALSE, ann=FALSE)"
puts {axis(1, at=10*1:10000)}
puts {axis(2, las=1, at=50*0:max_y)}
puts box()

foreach i [lrange $stocks 1 end] {
  puts "lines(stock_data\$$i, type=\"o\", col=plot_colors\[[incr j]\], ylim=c(0,max_y))"
}

puts {title(xlab="Seconds", col.lab=rgb(1, 0, 0))}
puts {title(ylab="Price", col.lab=rgb(1, 0, 0))}
puts {legend(0, max_y, names(stock_data), cex=0.8, col=plot_colors, pch=21:23, lty=1:3)}

puts dev.off
